package com.quasar.hpatient.view.record;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Handler;
import android.text.Editable;
import android.text.InputFilter;
import android.text.InputType;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.bigkoo.pickerview.OptionsPickerView;
import com.quasar.hpatient.MainAppliaction;
import com.quasar.hpatient.R;
import com.quasar.hpatient.base.BaseActivity;
import com.quasar.hpatient.constant.ConstantManger;
import com.quasar.hpatient.model.custommodel.DayItemRecord;
import com.quasar.hpatient.model.custommodel.ItemValueData;
import com.quasar.hpatient.model.httpmodel.AnotherResult;
import com.quasar.hpatient.model.httpmodel.DataStateResult;
import com.quasar.hpatient.model.litePalmodel.DailyItemInfodb;
import com.quasar.hpatient.model.medicalmodel.DailyRecordDetailsData;
import com.quasar.hpatient.onInterface.OnDailyMoreListener;
import com.quasar.hpatient.onInterface.OndailyRecordListener;
import com.quasar.hpatient.presenter.DailyMorePresenter;
import com.quasar.hpatient.presenter.DailyRecordPresenter;
import com.quasar.hpatient.utils.OutliersValues;
import com.quasar.hpatient.utils.TimeZoneUtil;
import com.quasar.hpatient.widget.CustomFAB;
import com.quasar.hpatient.widget.Widget_DayRecord_Item;
import com.vise.log.ViseLog;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.Extra;
import org.androidannotations.annotations.OptionsItem;
import org.androidannotations.annotations.OptionsMenu;
import org.androidannotations.annotations.ViewById;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

/**
 * 更多日常记录
 */
@OptionsMenu(R.menu.menu_more_recode)
@EActivity(R.layout.activity_more_recode)

public class MoreRecodeActivity extends BaseActivity implements OnDailyMoreListener, Widget_DayRecord_Item.OnRemove, OndailyRecordListener {

    private DailyMorePresenter dailyMorePresenter;
    private DailyRecordPresenter dailyRecordPresenter;
    boolean isClick = true;
    //检测日常数据里面的异常值
    boolean warn = true;
    @Extra
    String TimeDate;
    @ViewById
    LinearLayout layout;
    @ViewById(R.id.tv_display_date)//当前时间
            TextView tv_display_date;
    //悬浮按钮
    @ViewById(R.id.cf_imageButton)
    CustomFAB cf_imageButton;

    List<DailyItemInfodb> dailyItemInfoDBList = MainAppliaction.getInstance().getDailyItemInfoList();


    @AfterViews
    void init() {
        initView();
        initData();
    }

    String Time = null;

    @Override
    protected void initView() {
        initToolbar();
        if (TimeDate != null) {
            Time = TimeDate.substring(0, 10);
        } else {
            Time = TimeZoneUtil.sdf.format(new Date()).substring(0, 10);
        }
        tv_display_date.setText(Time);
    }


    @Override
    protected void initData() {
        dailyMorePresenter = new DailyMorePresenter(this);
        dailyRecordPresenter = new DailyRecordPresenter(this);

    }

    @Override
    protected void onResume() {
        super.onResume();
        stats = -1;
        dailyMorePresenter.dailyMoreData();
    }

    List<DailyItemInfodb> localData = new ArrayList<>();

    @Override
    public void onSuccessSameday(List<DailyItemInfodb> data) {//获取数据库中的日常记录
        dessDialog();
        layout.removeAllViews();
        String time = tv_display_date.getText().toString();
        localData.clear();

        DailyItemInfodb dailyItemInfodb = new DailyItemInfodb(
                0, 0, "夜尿量",
                "", "",
                "毫升(ml)", 0, "1",
                "", new Date(),
                new Date(), 1, 1, 1);


        localData.addAll(data);
        localData.add(6, dailyItemInfodb);
        if (stats == -1) {
            dailyMorePresenter.GetPatientDailySingleRecords(time);
        } else if (stats == 0) {
            dailyMorePresenter.GetPatientDailySingleRecords(TimeZoneUtil.pre(time, stats));
        } else if (stats == 2) {
            dailyMorePresenter.lastDailyRecordListData(time);
            dessDialog();
        } else if (stats == 3) {
            dailyMorePresenter.GetNextDailyRecordListData(time);
        } else {
            dailyMorePresenter.GetPatientDailySingleRecords(TimeZoneUtil.pre(time, stats));
        }
        showDialog();

    }

    @Override
    public void onError(String msg) {
        if (msg != null) {
            msg(msg);
        }
        isClick = true;
        isClickBoolean = false;
        dessDialog();
    }

    @Override
    public void onRefreshUI() {
        dessDialog();
        isClick = true;
        warn = true;

    }

    //提交成功后的返回
    @Override
    public void onSubmitSuccess(DataStateResult dataStateResult) {
        dessDialog();
        if (dataStateResult.getMsgCode().equals("0x00000000")) {
            stats = -1;
            msg("提交成功");
            layout.removeAllViews();
            dailyMorePresenter = new DailyMorePresenter(this);
            dailyMorePresenter.dailyMoreData();
        }
    }

    @Override
    public void onUpdateValueSuccess(DataStateResult dataStateResult) {

    }

    @Override
    public void onDeleteValueSuccess(DataStateResult dataStateResult) {

    }

    //添加日常记录项
    @Click(R.id.cf_imageButton)
    public void cf_imageButton() {
        ShowPickerView();
    }

    boolean isClickPickView = true;

    private void ShowPickerView() {// 弹出选择器`
        final String Time = tv_display_date.getText().toString();

        final List<DayItemRecord> DayList = new ArrayList<>();
        final List<DayItemRecord> delete = new ArrayList<>();

        int count = layout.getChildCount();
        for (int i = 0; i <= count; i++) {
            View view = layout.getChildAt(i);
            if (view instanceof Widget_DayRecord_Item) {
                Widget_DayRecord_Item x = (Widget_DayRecord_Item) view;
                ViseLog.d("检测是否删除++++项" + x.getItem().getMarkValue());
                DayList.add(x.getItem());
                if (x.getItem().getMarkValue() != null) {
                    if (x.getItem().getMarkValue().equals("删除")) {
                        delete.add(x.getItem());

                        ViseLog.d("检测是否删除，项" + x.getItem().getDailyitemName());
                    }
                }
            }
        }
        //选择日常检查项目
        OptionsPickerView pvOptions = new OptionsPickerView.Builder(this, new OptionsPickerView.OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(final int options1, int options2, int options3, View v) {
//                if (isClickPickView) {
                isClickPickView = false;
                DayItemRecord tx = delete.get(options1);

                ViseLog.d("///////////////////////////////////" + tx);
                for (int i = 0; i < DayList.size(); i++) {
                    if (DayList.get(i).getDailyitemName().equals(tx.getDailyitemName())) {
                        DayList.get(i).setMarkValue("");
                    }
                }

                ViseLog.d("///////////////////////////////////");
                layout.removeAllViews();
                for (int i = 0; i < DayList.size(); i++) {
                    Widget_DayRecord_Item widget_dayRecord_item = new Widget_DayRecord_Item(MoreRecodeActivity.this);
                    widget_dayRecord_item.setTestingDate(tv_display_date.getText().toString());
                    widget_dayRecord_item.setItem(DayList.get(i));
                    widget_dayRecord_item.setRemove(MoreRecodeActivity.this);
                    layout.addView(widget_dayRecord_item);
                }
//                }
            }
        })
                .setTitleText("日常检查项")
                .setDividerColor(Color.BLACK)
                .setTextColorCenter(Color.BLACK) //设置选中项文字颜色
                .setContentTextSize(20)
                .setOutSideCancelable(false)// default is true
                .build();
        pvOptions.setPicker(delete);//三级选择器

        if (delete.size() != 0) {
            pvOptions.show();
        } else {
            msg("已经添加全部日常检查项");
        }

    }

    //新增item
    @Override
    public void onItemSuccess(DataStateResult dataStateResult) {

    }

    //删除item
    @Override
    public void ondeleteSuccess(DataStateResult dataStateResult) {
        if (dataStateResult.getMsgCode().equals("0x00000000")) {
            msg("删除成功");
        }
    }


    //获取服务器的数值
    List<DailyItemInfodb> postdai = new ArrayList<>();

    //获取服务器提交过的日常检查项
    @Override
    public void onDailyInspect(AnotherResult<DailyRecordDetailsData> index) {
        ListDataTime(index);
        dessDialog();
    }

    public void ListDataTime(AnotherResult<DailyRecordDetailsData> index) {
        //记录身高
        String height = null;
        String weight = null;
        String DataTime = null;


        dessDialog();

        int size = index.getList().size();
        int count = localData.size();//本地数据
        for (int i = 0; i < count; i++) {
            for (int j = 0; j < size; j++) {
                if (localData.get(i).getName().equals(index.getList().get(j).getDailyitemName())) {
                    localData.get(i).setPostid(index.getList().get(j).getId());
                    localData.get(i).setPaired(index.getList().get(j).getPaired());
                    localData.get(i).setVal(index.getList().get(j).getLogValue());
                    localData.get(i).setMarkValue(index.getList().get(j).getMarkValue());
                    if (localData.get(i).getName().equals("身高")) {
                        height = index.getList().get(j).getLogValue();
                    } else if (localData.get(i).getName().equals("体重")) {
                        weight = index.getList().get(j).getLogValue();
                    }

                    if (index.getList().get(j).getDailyRecordIndex() != null) {
                        if (index.getList().get(j).getDailyRecordIndex().getDailyLogBeginTime() != null) {
                            DataTime = index.getList().get(j).getDailyRecordIndex().getDailyLogBeginTime();
                        }
                    }
                }
            }
        }


        ViseLog.d("计算的ibm值" + height + weight);

        if (index.getList() != null && index.getList().size() > 0) {
            if (DataTime != null) {
                tv_display_date.setText(DataTime.substring(0, 10));
            }
        }

        postdai.addAll(localData);

        String Time = tv_display_date.getText().toString();
        for (int i = 0; i < localData.size(); i++) {
            DayItemRecord record = new DayItemRecord();
            record.setItemId(localData.get(i).getPrimaryid() + "");//itemID
            if (localData.get(i).getMarkValue() != null) {
                record.setMarkValue(localData.get(i).getMarkValue());
            }
            //参考值
            record.setReference(localData.get(i).getStandard());
            record.setDailyitemName(localData.get(i).getName());

            record.setDailyitemDose(localData.get(i).getDimensionid());//单位
            record.setId(localData.get(i).getPostid() + "");
            record.setLogType(localData.get(i).getLogType());
            if (localData.get(i).getPaired() != null) {
                record.setPaired(localData.get(i).getPaired());
            } else {
                record.setPaired("");
            }
            if (localData.get(i).getName().equals("BMI")) {
                ViseLog.d("计算的ibm值+++++" + localData.get(i).getName());
                if (height != null && weight != null) {
                    float weightbmi = Float.parseFloat(weight);
                    float heightbmi = Float.parseFloat(height);
                    if (heightbmi != 0 && weightbmi != 0) {
                        double value = weightbmi / ((heightbmi * 0.01) * (0.01 * heightbmi));
                        BigDecimal bd = new BigDecimal(value);
                        bd = bd.setScale(1, BigDecimal.ROUND_HALF_UP);//BigDecimal.ROUND_HALF_UP 是4舍5入，BigDecimal.ROUND_DOWN是舍去，BigDecimal.ROUND_FLOOR是向上取整
                        localData.get(i).setVal(bd + "");
                    }
                    ViseLog.d("计算的ibm值" + localData.get(i).getName());
                    record.setLogValue(localData.get(i).getVal());
                }
            } else {
                record.setLogValue(localData.get(i).getVal());
            }

            Widget_DayRecord_Item widget_dayRecord_item = new Widget_DayRecord_Item(this);
            widget_dayRecord_item.setTestingDate(tv_display_date.getText().toString());
            widget_dayRecord_item.setItem(record);
            widget_dayRecord_item.setRemove(MoreRecodeActivity.this);
            layout.addView(widget_dayRecord_item);
        }
        isClickBoolean = false;

        final String type = "1";
        dailyRecordPresenter.GetUserOneDayDailyRecordData("出入量", tv_display_date.getText().toString(), type);
    }

    @Override
    public void onLineSuccess(AnotherResult<DailyRecordDetailsData> data) {

    }

    @Override
    public void onLineOneDaySuccess(AnotherResult<DailyRecordDetailsData> data) {

    }

    String isBMIA = null;
    String isBMIB = null;

    //新增
    @OptionsItem(R.id.action_add)
    public void action_add() {
        ShowPickerView();
    }


    //判断是否存在数据
    boolean isNotData;

    //保存
    @OptionsItem(R.id.action_preservation)
    public void action_preservation() {
        isNotData = false;
        List<DayItemRecord> DayList = new ArrayList<>();
        int count = layout.getChildCount();
        for (int i = 0; i < count; i++) {
            View view = layout.getChildAt(i);
            if (view instanceof Widget_DayRecord_Item) {
                Widget_DayRecord_Item x = (Widget_DayRecord_Item) view;
                DayList.add(x.getItem());
                if (x.getAbnormal() != 0) {
                    warn = false;
                }
            }
        }

        //判断是否填写数据了
        for (int i = 0; i < DayList.size(); i++) {
            DayItemRecord dayItemRecord = DayList.get(i);
            if (dayItemRecord.getLogValue() != null || dayItemRecord.getLogValue().length() > 0) {
                if (!dayItemRecord.getLogValue().equals("")) {
                    isNotData = true;
                    break;
                } else {
                    try {
                        float data = Float.parseFloat(dayItemRecord.getLogValue());
                        if (data <= 0) {
                            isNotData = true;
                            break;
                        }
                    } catch (Exception e) {
                        e.getMessage();
                    }
                }
            }
        }

        List<ItemValueData> itemList = new ArrayList<>();

        for (int i = 0; i < DayList.size(); i++) {
            ItemValueData item = new ItemValueData();
            item.setLogValue(DayList.get(i).getLogValue() + "");
            item.setDailyitemDose(DayList.get(i).getDailyitemDose());
            item.setLogType(DayList.get(i).getLogType());
            if (DayList.get(i).getMarkValue() != null) {
                item.setMarkValue(DayList.get(i).getMarkValue());
            } else {
                item.setMarkValue("");
            }

            if (DayList.get(i).getDailyitemName().equals("身高")) {
                if (DayList.get(i).getLogValue() != null && DayList.get(i).getLogValue().length() > 0) {
                    isBMIA = DayList.get(i).getLogValue();

                    ViseLog.d("身高");
                }
            }

            if (DayList.get(i).getDailyitemName().equals("体重")) {
                if (DayList.get(i).getLogValue() != null && DayList.get(i).getLogValue().length() > 0) {
                    isBMIB = DayList.get(i).getLogValue();
                    ViseLog.d("体重");
                }
            }


            if (DayList.get(i).getPaired() != null) {
                item.setPaired(DayList.get(i).getPaired());
            } else {
                item.setPaired("");
            }

            if (DayList.get(i).getDailyitemName().equals(ConstantManger.OUTSUM) ||
                    DayList.get(i).getDailyitemName().equals(ConstantManger.INSUM) ||
                    DayList.get(i).getDailyitemName().equals(ConstantManger.night) ||
                    DayList.get(i).getDailyitemName().equals(ConstantManger.TEMPERATURENAME) ||
                    DayList.get(i).getDailyitemName().equals(ConstantManger.BLOODPRESSURENAME) ||
                    DayList.get(i).getDailyitemName().equals(ConstantManger.HEARTRATENAME) ||
                    DayList.get(i).getDailyitemName().equals(ConstantManger.BILLDSUGAR)
                    ) {
                continue;
            }

            //检测不可能值停止提交
            if (DayList.get(i).getLogValue() != null && DayList.get(i).getLogValue().length() > 0) {
                for (int j = 0; j < dailyItemInfoDBList.size(); j++) {
                    //只判断身高，体重，睡眠时间
                    if (dailyItemInfoDBList.get(j).getName().equals("身高")
                            || dailyItemInfoDBList.get(j).getName().equals("体重")
                            || dailyItemInfoDBList.get(j).getName().equals("睡眠时间")
                            || dailyItemInfoDBList.get(j).getName().equals("运动量")
                            || dailyItemInfoDBList.get(j).getName().equals("引流量")) {
                        if (dailyItemInfoDBList.get(j).getName().equals(DayList.get(i).getDailyitemName())) {
                            int textColor = OutliersValues.TextColor(
                                    DayList.get(i).getLogValue() + "",
                                    dailyItemInfoDBList.get(j).getStandard());

                            //移除对bmi的检测
                            if (!ConstantManger.BMI.equals(DayList.get(i).getDailyitemName())) {
                                if (textColor != 0) {
                                    warn = false;
                                    msg(dailyItemInfoDBList.get(j).getName() + "检测值为不可能值");
                                    return;
                                }
                            }
                        }
                    }
                }
            }
            item.setDailyitemName(DayList.get(i).getDailyitemName());
            itemList.add(item);
        }


        if (isBMIB != null && isBMIA != null) {

            if (OutliersValues.editTextNum(isBMIB) && OutliersValues.editTextNum(isBMIA)) {
                float tiZhong = Float.parseFloat(isBMIB);
                float shenGao = Float.parseFloat(isBMIA);
                ViseLog.d("对BMI进行计算" + tiZhong + "--" + shenGao);

                if (shenGao != 0 && tiZhong != 0) {
                    double value = tiZhong / ((shenGao * 0.01) * (0.01 * shenGao));
                    BigDecimal bd = new BigDecimal(value);
                    bd = bd.setScale(1, BigDecimal.ROUND_HALF_UP);//BigDecimal.ROUND_HALF_UP 是4舍5入，BigDecimal.ROUND_DOWN是舍去，BigDecimal.ROUND_FLOOR是向上取整
                    ViseLog.d("计算出来的bmi" + bd + "**" + value);
                    for (int z = 0; z < itemList.size(); z++) {
                        if (itemList.get(z).getDailyitemName().equals(ConstantManger.BMI)) {
                            itemList.get(z).setLogValue(bd + "");
                        }
                    }
                }
            }
        }

        //判断没有数据不能进行提交
        if (!isNotData) {
            msg("请填写日常数据");
            return;
        }


        String json = JSON.toJSONString(itemList);
        ViseLog.d("移交的json" + json);

        if (itemList.size() == 0) {
            msg("请添加日常检查项");
            return;
        }
        String TimeDate = tv_display_date.getText().toString();
        showDialog();
        String warns = null;
        if (warn) {
            warns = ConstantManger.userType;
        } else {
            warns = ConstantManger.EditType;
        }
        if (isClick) {
            isClick = false;
            //提交日常检查项目
            dailyMorePresenter.moreAddDailyRecordData(json,
                    TimeDate, warns);//0代表数据正常，1代表异常
        }
    }

    @Override
    public void remove(Widget_DayRecord_Item item) {

        ViseLog.d("删除");
        if (item.getItem().getLogType() == 0) {
            msg("默认项不能删除");
        } else {
            item.hideView();
            item.dayItemRecord.setMarkValue("删除");
        }
    }

    int stats = -1;//表示不變

    //上一天
    @Click(R.id.pre)
    public void pre() {
        if (isClickBoolean) {
            return;
        }
        postdai.clear();
        String time = tv_display_date.getText().toString();
        stats = 0;
        isClickBoolean = true;
        dailyMorePresenter.dailyMoreData();
        tv_display_date.setText(TimeZoneUtil.pre(time, stats));

    }

    //下一天
    @Click(R.id.next)
    public void next() {
        if (isClickBoolean) {
            return;
        }
        String time = tv_display_date.getText().toString();
        ViseLog.d("下一天的数据++++++" + time);
        if (!time.equals(TimeZoneUtil.sdfFormat.format(new Date()))) {
            postdai.clear();
            stats = 1;
            isClickBoolean = true;
            dailyMorePresenter.dailyMoreData();
            tv_display_date.setText(TimeZoneUtil.pre(time, stats));
            ViseLog.d("下一天的数据" + time);
        }
    }

    //快速点击上一次下一次数据
    boolean isClickBoolean = false;

    //上一次
    @Click(R.id.iv_last)
    public void iv_last() {
        if (isClickBoolean) {
            return;
        }
        postdai.clear();
        stats = 2;
        isClickBoolean = true;
        dailyMorePresenter.dailyMoreData();

    }

    //下一次
    @Click(R.id.iv_next)
    public void iv_next() {
        if (isClickBoolean) {
            return;
        }
        String time = tv_display_date.getText().toString();
        if (!time.equals(TimeZoneUtil.sdfFormat.format(new Date()))) {
            postdai.clear();
            stats = 3;
            isClickBoolean = true;
            dailyMorePresenter.dailyMoreData();
        }
    }

    @Override
    public void onLastOneDaySuccess(AnotherResult<DailyRecordDetailsData> dataAnotherResult) {
        //如果上次没有数据查找当前的数据
        if (dataAnotherResult.getList().size() == 0) {
            stats = -1;
            dailyMorePresenter.dailyMoreData();
        } else {
            ListDataTime(dataAnotherResult);
        }
        dessDialog();

    }

    @Override
    public void onNextOneDaySuccess(AnotherResult<DailyRecordDetailsData> dataAnotherResult) {
        //如果上次没有数据查找当前的数据
        if (dataAnotherResult.getList().size() == 0) {
            stats = -1;
            dailyMorePresenter.dailyMoreData();
        } else {
            ListDataTime(dataAnotherResult);
        }
        dessDialog();
    }


    //编辑
    @Override
    public void edittextVal(Widget_DayRecord_Item item) {
        if (item.getItem() != null) {
            DayItemRecord dayItemRecord = item.getItem();
            if (dayItemRecord != null) {
                heightWight();
            }
        }
    }

    @Override
    public void OtherVal(Widget_DayRecord_Item item) {
        String timeDate = tv_display_date.getText().toString();
        if (item.getItem() != null) {
            DayItemRecord dayItemRecord = item.getItem();
            if (dayItemRecord.getDailyitemName().equals(ConstantManger.TEMPERATURENAME)) {//体温
                TemperatureActivity_.intent(this).TimeVal(timeDate).start();

            } else if (dayItemRecord.getDailyitemName().equals(ConstantManger.BLOODPRESSURENAME)) {//血压
                BloodPressureActivity_.intent(this).TimeVal(timeDate).start();

            } else if (dayItemRecord.getDailyitemName().equals(ConstantManger.HEARTRATENAME)) {//心率
                HeartRateActivity_.intent(this).TimeVal(timeDate).start();

            } else if (dayItemRecord.getDailyitemName().equals(ConstantManger.BILLDSUGAR)) {//血糖
                BloodSugarActivity_.intent(this).TimeVal(timeDate).start();

            } else if (dayItemRecord.getDailyitemName().equals(ConstantManger.OUTSUM) ||
                    dayItemRecord.getDailyitemName().equals(ConstantManger.INSUM) ||
                    dayItemRecord.getDailyitemName().equals(ConstantManger.night)) {//出入量，
                IntakeOutputActivity_.intent(this).TimeVal(timeDate).start();

            }
        }
    }

    //修改体重和身高的值
    public void heightWight() {

        List<DayItemRecord> DayList = new ArrayList<>();
        int count = layout.getChildCount();
        for (int i = 0; i < count; i++) {
            View view = layout.getChildAt(i);
            if (view instanceof Widget_DayRecord_Item) {
                Widget_DayRecord_Item x = (Widget_DayRecord_Item) view;
                DayList.add(x.getItem());
            }
        }


        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("根据身高体重计算IBM值");
        View views = LayoutInflater.from(this).inflate(R.layout.dialog_edit_daily, null);


        builder.setView(views);
        final EditText edit_min = (EditText) views.findViewById(R.id.edit_min);//身高
        final EditText edit_max = (EditText) views.findViewById(R.id.edit_max);//体重
        edit_min.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL | EditorInfo.TYPE_CLASS_PHONE);
        edit_min.setFilters(new InputFilter[]{new InputFilter.LengthFilter(5)});
        edit_max.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL | EditorInfo.TYPE_CLASS_PHONE);
        edit_max.setFilters(new InputFilter[]{new InputFilter.LengthFilter(5)});


        String heightVal = null;
        String weightVal = null;
        for (int i = 0; i < DayList.size(); i++) {
            if (DayList.get(i).getDailyitemName().equals(ConstantManger.height)) {
                heightVal = DayList.get(i).getLogValue();
            } else if (DayList.get(i).getDailyitemName().equals(ConstantManger.weight)) {
                weightVal = DayList.get(i).getLogValue();
            }
        }

        if (heightVal != null && heightVal.length() > 0) {
            edit_min.setText(heightVal);
        }


        if (weightVal != null && weightVal.length() > 0) {
            edit_max.setText(weightVal);
        }

        //身高
        edit_min.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {

                if (s.toString().toString().indexOf(".") >= 0) {
                    edit_min.setFilters(new InputFilter[]{new InputFilter.LengthFilter(5)});
                } else {
                    edit_min.setFilters(new InputFilter[]{new InputFilter.LengthFilter(4)});
                }

                if (OutliersValues.editTextNum(s.toString().trim())) {
                    if (s.toString() != null && s.toString().length() > 0) {

                        float val1 = Float.parseFloat(s.toString());
                        if (val1 > 300.1) {
                            Toast.makeText(MoreRecodeActivity.this, "身高检测值异常", Toast.LENGTH_SHORT).show();
                            edit_min.setText("");
                        }
                    }
                }
            }
        });

        //体重
        edit_max.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {


                if (s.toString().toString().indexOf(".") >= 0) {
                    edit_max.setFilters(new InputFilter[]{new InputFilter.LengthFilter(5)});
                } else {
                    edit_max.setFilters(new InputFilter[]{new InputFilter.LengthFilter(4)});
                }

                if (OutliersValues.editTextNum(s.toString().trim())) {
                    if (s.toString() != null && s.toString().length() > 0) {

                        float val1 = Float.parseFloat(s.toString());
                        if (val1 > 300.1) {
                            Toast.makeText(MoreRecodeActivity.this, "体重检测值异常", Toast.LENGTH_SHORT).show();
                            edit_max.setText("");
                        }
                    }
                }
            }
        });

        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                //确定后操作
                try {
                    String min_val = edit_min.getText().toString();
                    String max_val = edit_max.getText().toString();

                    edittextWidget(min_val + "", max_val + "");
                } catch (Exception e) {
                    Toast.makeText(MoreRecodeActivity.this, "身高或者体重检测值异常", Toast.LENGTH_SHORT).show();
                }
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {

            }
        });

        Timer timer = new Timer();
        timer.schedule(new TimerTask() {

            @Override
            public void run() {
                showKeyboard(edit_min);
            }
        }, 300);
        builder.show();

    }

    public void showKeyboard(EditText editText) {
        if (editText != null) {
            //设置可获得焦点
            editText.setFocusable(true);
            editText.setFocusableInTouchMode(true);
            //请求获得焦点
            editText.requestFocus();
            //调用系统输入法
            InputMethodManager inputManager = (InputMethodManager) editText
                    .getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
            inputManager.showSoftInput(editText, 0);
        }
    }

    //修改控件上面的值
    public void edittextWidget(String height, String weight) {

        BigDecimal bd = null;
        if (height != null && height.length() > 0 && weight != null && weight.length() > 0) {
            float weightbmi = Float.parseFloat(weight);
            float heightbmi = Float.parseFloat(height);
            if (heightbmi != 0 && weightbmi != 0) {
                double value = weightbmi / ((heightbmi * 0.01) * (0.01 * heightbmi));
                bd = new BigDecimal(value);
                bd = bd.setScale(1, BigDecimal.ROUND_HALF_UP);//BigDecimal.ROUND_HALF_UP 是4舍5入，BigDecimal.ROUND_DOWN是舍去，BigDecimal.ROUND_FLOOR是向上取整
            }
        }

        List<DayItemRecord> DayList = new ArrayList<>();
        int count = layout.getChildCount();
        for (int i = 0; i < count; i++) {
            View view = layout.getChildAt(i);
            if (view instanceof Widget_DayRecord_Item) {
                Widget_DayRecord_Item x = (Widget_DayRecord_Item) view;
                DayList.add(x.getItem());
            }
        }

        for (int i = 0; i < DayList.size(); i++) {
            DayItemRecord dayItemRecord = DayList.get(i);
            //身高
            if (dayItemRecord.getDailyitemName().equals(ConstantManger.height)) {
                if (height != null && height.length() > 0) {
                    dayItemRecord.setLogValue(height + "");
                }
            } else if (dayItemRecord.getDailyitemName().equals(ConstantManger.weight)) {//体重
                if (weight != null && weight.length() > 0) {
                    dayItemRecord.setLogValue(weight + "");
                }
            } else if (dayItemRecord.getDailyitemName().equals(ConstantManger.BMI)) {//计算bmi
                if (bd != null) {
                    dayItemRecord.setLogValue(bd + "");
                    ViseLog.d("计算出的bmi值" + bd);
                }
            }
        }

        layout.removeAllViews();

        for (int i = 0; i < DayList.size(); i++) {
            Widget_DayRecord_Item widget_dayRecord_item = new Widget_DayRecord_Item(this);
            widget_dayRecord_item.setTestingDate(tv_display_date.getText().toString());
            widget_dayRecord_item.setItem(DayList.get(i));
            widget_dayRecord_item.setRemove(MoreRecodeActivity.this);
            layout.addView(widget_dayRecord_item);
        }
    }

    @Override
    public void onDailyRecordDetailsSuccess(AnotherResult<DailyRecordDetailsData> data) {
        int outSum = 0;
        int inSum = 0;
        int isnight = 0;

        try {
            for (int i = 0; i < data.getList().size(); i++) {
                if (data.getList().get(i).getMarkValue().equals("夜尿量")) {
                    isnight = isnight + Integer.parseInt(data.getList().get(i).getLogValue());
                }
                outSum = outSum + Integer.parseInt(data.getList().get(i).getLogValue());
                inSum = inSum + Integer.parseInt(data.getList().get(i).getPaired());
            }
        }catch (Exception E){

        }

        List<DayItemRecord> DayList = new ArrayList<>();
        int count = layout.getChildCount();
        for (int i = 0; i < count; i++) {
            View view = layout.getChildAt(i);
            if (view instanceof Widget_DayRecord_Item) {
                Widget_DayRecord_Item x = (Widget_DayRecord_Item) view;
                DayList.add(x.getItem());
            }
        }

        for (int i = 0; i < DayList.size(); i++) {
            DayItemRecord dayItemRecord = DayList.get(i);
            if (DayList.get(i).getDailyitemName().equals(ConstantManger.OUTSUM)) {
                dayItemRecord.setLogValue(outSum + "");
            } else if (DayList.get(i).getDailyitemName().equals(ConstantManger.INSUM)) {
                dayItemRecord.setLogValue(inSum + "");
            } else if (DayList.get(i).getDailyitemName().equals(ConstantManger.night)) {
                dayItemRecord.setLogValue(isnight + "");
            }
        }

        layout.removeAllViews();

        for (int i = 0; i < DayList.size(); i++) {
            Widget_DayRecord_Item widget_dayRecord_item = new Widget_DayRecord_Item(this);
            widget_dayRecord_item.setTestingDate(tv_display_date.getText().toString());
            widget_dayRecord_item.setItem(DayList.get(i));
            widget_dayRecord_item.setRemove(MoreRecodeActivity.this);
            layout.addView(widget_dayRecord_item);
        }

    }
}